package com.enjoy.adv.client;

import com.enjoy.adv.kryocodec.kryoDecoder;
import com.enjoy.adv.kryocodec.kryoEncoder;
import com.enjoy.adv.server.HeartBeatRespHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.timeout.ReadTimeoutHandler;

/**
 * Created by HP on 2019/7/19.
 */
public class ClientInit extends ChannelInitializer<SocketChannel>{

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        /*剥离接受到的消息长度字段*/
        ch.pipeline().addLast("frameDecoder",new LengthFieldBasedFrameDecoder(65535,0,2,0,2))
                //发送出去的消息增加长度字段
                .addLast("frameEncoder",new LengthFieldPrepender(2))
                /*反序列化 将字节数据组转换为消息实体*/
                .addLast(new kryoDecoder())
                /*消息序列化为数组*/
                .addLast(new kryoEncoder())
                //超时检测
                .addLast(new ReadTimeoutHandler(10))
                /*发出登录请求*/
                .addLast(new LoginAuthReqHandler())
                /*发出心跳请求*/
                .addLast(new HeartBeatReqHandler());
    }
}
